/*
 * @Author: 生俊甫 1758142861@qq.com
 * @Date: 2024-10-07 17:06:26
 * @LastEditors: 生俊甫 1758142861@qq.com
 * @LastEditTime: 2024-10-07 17:15:09
 * @FilePath: /sjf/2024-project/2024_centos/test_to_c/stack_and_queue/stack_test.c
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
#include "stack_test.h"

void stack_init(stack* s)
{
    assert(s);
    s->_a = (STDataType*)malloc(sizeof(STDataType)* 4);
    s->_capacity = 4;
    s->_top = 0;//初始为0
}
void stack_destory(stack* s)
{
    assert(s);
    free(s);
    s->_a = NULL;
    s->_top = s->_capacity = 0;
}
void push(stack* s,STDataType x)
{
    assert(s);
    //栈满扩容
    if(s->_top == s->_capacity)
    {
        STDataType* new_s = (STDataType*)realloc(s->_a,(4*s->_capacity) * sizeof(STDataType));
        s->_a = new_s;
        s->_capacity *= 4;
    }
    s->_a[s->_top] = x;
    s->_top++;
}
void pop(stack* s)
{
    assert(s);
    assert(!stack_empty(s));
    s->_top--;
}
int stack_top(stack* s)
{
    return s->_a[s->_top];
}
bool stack_empty(stack* s)
{
    return s->_top == 0;
}
int stack_size(stack* s)
{
    return s->_top;
}